package wox.lonice.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wox.lonice.dao.WeArticleInfoMapper;
import wox.lonice.entity.WeArticleInfo;
import wox.lonice.entity.WeArticleInfoExample;
import wox.lonice.service.WeArticleInfoService;

import java.util.List;

@Service
public class WeArticleInfoServiceImpl implements WeArticleInfoService {

    @Autowired
    private WeArticleInfoMapper weArticleInfoMapper;

    @Override
    public WeArticleInfo selectByAppmsgid(String appmsgid){
        WeArticleInfoExample example = new WeArticleInfoExample();
        example.createCriteria().andAppmsgidEqualTo(appmsgid);
        List<WeArticleInfo> weArticleInfos = weArticleInfoMapper.selectByExample(example);
        if(CollectionUtil.isNotEmpty(weArticleInfos)){
            return weArticleInfos.get(0);
        }
        return null;
    }

    @Override
    public void saveAll(List<WeArticleInfo> weArticleInfos) {
        weArticleInfos.forEach(weArticleInfo -> {
            WeArticleInfo info = this.selectByAppmsgid(weArticleInfo.getAppmsgid());
            if(ObjectUtil.isNotNull(info) && info.equals(weArticleInfo)){
                info.setAid(weArticleInfo.getAid()).setAppmsgid(weArticleInfo.getAppmsgid())
                        .setTitle(weArticleInfo.getTitle()).setDigest(weArticleInfo.getDigest())
                        .setItemShowType(weArticleInfo.getItemShowType()).setTagid(weArticleInfo.getTagid())
                        .setCover(weArticleInfo.getCover()).setLink(weArticleInfo.getLink());
                weArticleInfoMapper.updateByPrimaryKeySelective(info);
            } else {
                weArticleInfoMapper.insert(weArticleInfo);
            }
        });
    }
}
